<template>
    <div class="app-container">
        <header-title/>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
            <el-form-item label="关键词" prop="keyword">
                <el-input
                    v-model="queryParams.keyword"
                    placeholder="请输入菜单名称/跳转路径"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            
            
            <el-form-item label="是否需要登录" prop="isNeedLogin">
                <el-select v-model="queryParams.isNeedLogin" clearable>
                    <el-option :value="0" label="否"></el-option>
                    <el-option :value="1" label="是"></el-option>

                </el-select>
            </el-form-item>
            <el-form-item label="ai服务类型" prop="aiType">
                <el-select
                    v-model="queryParams.aiType"
                    placeholder="请选择ai服务类型"
                    clearable
                >
                    <el-option 
                        v-for="dict in dict.type.ai_type" 
                        :label="dict.label" 
                        :key="dict.value" 
                        :value="dict.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="等级" prop="firstType">
                <el-select
                    v-model="queryParams.firstType"
                    placeholder="请选择一级类型"
                    clearable
                    @change="changeFirstType"
                    style="width: 150px;margin-right: 10px;"
                >
                    <el-option 
                        v-for="dict in dict.type.first_category" 
                        :label="dict.label" 
                        :key="dict.value" 
                        :value="dict.value">
                    </el-option>
                </el-select>
                <el-select
                    v-model="queryParams.secondType"
                    placeholder="请选择二级类型"
                    clearable
                    style="width: 150px;"
                >
                    <el-option 
                        v-for="dict in dict.type.industry_trends" 
                        :label="dict.label" 
                        :key="dict.value" 
                        v-if="queryParams.firstType == 1"
                        :value="dict.value">
                    </el-option>
                    <el-option 
                        v-for="dict in dict.type.self_media" 
                        :label="dict.label" 
                        :key="dict.value" 
                        v-if="queryParams.firstType == 2"
                        :value="dict.value">
                    </el-option>
                    <el-option 
                        v-for="dict in dict.type.customer_acquisition" 
                        :label="dict.label" 
                        :key="dict.value" 
                        v-if="queryParams.firstType == 3"
                        :value="dict.value">
                    </el-option>
                </el-select>
                <!-- <el-select v-model="queryParams.type" clearable>
                    <el-option
                        v-for="dict in dict.type.tutorial_type"
                        :label="dict.label"
                        :value="dict.value"
                        :key="dict.value"
                    ></el-option>
                </el-select> -->
            </el-form-item>
            <el-form-item class="mar-r-20">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"
                >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>

            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="tableList">
            <el-table-column label="ID" align="center" prop="appMenuId" width="60"/>
            <el-table-column label="菜单名称" align="center" prop="name" min-width="100"/>
            <el-table-column label="图标" align="center">
                <template slot-scope="scope">
                    <imagePreview :url="scope.row.icon"/>
                </template>
            </el-table-column>
            <el-table-column label="跳转路径" align="center" prop="path" min-width="120"/>
            <el-table-column label="分类名称" align="center" min-width="160">
                <template slot-scope="scope">
                    <div>一级分类：{{ scope.row.firstTypeName || '-' }}</div>
                    <div>二级分类： {{ scope.row.secondTypeName || '-' }}</div>
                </template>
            </el-table-column>
            <el-table-column label="ai服务类型" align="center" min-width="100">
                <template slot-scope="scope">
                    {{selectDictLabel(dict.type.ai_type,scope.row.aiType ) ||'-'}}
                   
                </template>
            </el-table-column>
            <el-table-column label="需要登录" align="center">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.isNeedLogin == 0" type="danger">否</el-tag>
                    <el-tag v-if="scope.row.isNeedLogin == 1" type="success">是</el-tag>

                </template>
            </el-table-column>
            <el-table-column label="排序" align="center" prop="order" />
            <el-table-column label="状态" align="center">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.state"
                        :active-value="1"
                        :inactive-value="0"
                        @change="handleStatusChange(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="180">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-search"
                        @click="handleUpdateDetail(scope.row)"
                    >详情</el-button>
                    <el-button
                        type="text"
                        size="small"
                        icon="el-icon-edit"
                        @click="onHandleUpdate(scope.row)"
                    >编辑</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
        <el-dialog :title="title" :visible.sync="show" width="780px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px" :disabled="type == 'detail' ? true : false">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="一级类型" prop="firstType">
                            <el-select
                                v-model="form.firstType"
                                placeholder="请选择一级类型"
                                clearable
                                @change="changeFirstType"
                            >
                                <el-option 
                                    v-for="dict in dict.type.first_category" 
                                    :label="dict.label" 
                                    :key="dict.value" 
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="二级类型" prop="secondType">
                            <el-select
                                v-model="form.secondType"
                                placeholder="请选择二级类型"
                                clearable
                            >
                                <el-option 
                                    v-for="dict in dict.type.industry_trends" 
                                    :label="dict.label" 
                                    :key="dict.value" 
                                    v-if="form.firstType == 1"
                                    :value="dict.value">
                                </el-option>
                                <el-option 
                                    v-for="dict in dict.type.self_media" 
                                    :label="dict.label" 
                                    :key="dict.value" 
                                    v-if="form.firstType == 2"
                                    :value="dict.value">
                                </el-option>
                                <el-option 
                                    v-for="dict in dict.type.customer_acquisition" 
                                    :label="dict.label" 
                                    :key="dict.value" 
                                    v-if="form.firstType == 3"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="ai服务类型" prop="aiType">
                            <el-select
                                v-model="form.aiType"
                                placeholder="请选择ai服务类型"
                                clearable
                                filterable
                            >
                                <el-option 
                                    v-for="dict in dict.type.ai_type" 
                                    :label="dict.label" 
                                    :key="dict.value" 
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    
                    <el-col :span="24">
                        <el-form-item  label="菜单名称" prop="name">
                            <el-input  v-model="form.name" placeholder="请输入菜单名称"  />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="跳转路径" prop="path">
                            <el-input  v-model="form.path" placeholder="请输入跳转路径"  />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="图标" prop="icon">
                            <imageUpload v-model="form.icon" size="mini" :isJson="false" :limit="1"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="排序" prop="order">
                            <el-input  v-model="form.order" placeholder="请输入排序"  />
                        </el-form-item>
                    </el-col>
                    
                    <el-col :span="24">
                        <el-form-item  label="是否需要登录" prop="isNeedLogin">
                            <el-radio-group v-model="form.isNeedLogin">
                                <el-radio :label="0">否</el-radio>
                                <el-radio :label="1">是</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="状态" prop="state">
                            <el-radio-group v-model="form.state">
                                <el-radio :label="0">禁用</el-radio>
                                <el-radio :label="1">启用</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="type != 'detail'" >
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="show = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import { mapGetters } from 'vuex'

    import {listAppMenu,addAppMenu,updateAppMenu,delAppMenu,detailAppMenu} from '@/api/operateManage/menuList'
    export default{
        name:'MenuList',
        dicts: ['first_category','industry_trends','self_media','customer_acquisition','ai_type'],
        data() {
            return {
                // 遮罩层
                loading: true,
                showSearch: true,
                // 总条数
                total: 0,
                // 表格数据
                tableList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    firstType: '',
                    isNeedLogin:undefined,
                    keyword:'',
                    secondType:'',
                    aiType: ''
                },
                show: false,
                form: {},
                type: '',
                title: '',
                rules: {
                    firstType: [
                        { required: true, message: "一级菜单类型不能为空", trigger: "blur" }
                    ],
                    secondType: [
                        { required: true, message: "二级菜单不能为空", trigger: "blur" }
                    ],
                    order: [
                        { required: true, message: "排序不能为空", trigger: "blur" }
                    ],
                    name: [
                        { required: true, message: "菜单名称不能为空", trigger: "blur" }
                    ],
                    path: [
                        { required: true, message: "跳转路径不能为空", trigger: "blur" }
                    ],
                    icon: [
                        { required: true, message: "图标不能为空", trigger: "blur" }
                    ],
                    aiType: [
                        { required: true, message: "ai服务类型不能为空", trigger: "blur" }

                    ]
                },
                detailId: ''
            }
        },
        computed: {
            ...mapGetters(['userInfo'])
        },
        created() {
            this.getList()
        },
        methods: {
             // 任务状态修改
            handleStatusChange(row) {
                let text = row.state === "0" ? "启用" : "停用";
                this.$modal.confirm('确认要"' + text + '""' + row.name + '"菜单吗？').then(function() {
                    let jsons = JSON.parse(JSON.stringify(row))
                    console.log(jsons)
                    
                    return updateAppMenu(jsons);
                }).then(() => {
                    this.getList()
                    this.$modal.msgSuccess(text + "成功");
                }).catch(function() {
                    row.state = row.state === "0" ? "1" : "0";
                });
            },
            changeFirstType() {
                this.form.secondType = ''
            },
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if(this.detailId){
                            updateAppMenu(this.form).then(res => {
                                this.$modal.msgSuccess(res.msg)
                                this.show = false
                                this.getList()
                            })
                        }else{
                            addAppMenu(this.form).then(res => {
                                this.$modal.msgSuccess(res.msg)
                                this.show = false
                                this.getList()
                            })
                        }
                     }
                })
            },
            handleAdd() {
                this.type = 'add'
                this.title = '新增菜单'
                this.detailId = ''
                this.form = {
                    firstType: '',
                    secondType:'',
                    isNeedLogin: 1,
                    order: '',
                    path: '',
                    icon:'',
                    name: '',
                    aiType: '',
                    state : 1
                }
                this.show = true
            },
            handleUpdateDetail(row) {
                this.type = 'detail'
                this.detailId = row.appMenuId
                this.title = '菜单详情'
                detailAppMenu({id: row.appMenuId}).then(res=> {
                    this.form = res.data
                    this.form.type = Number(res.data.type)
                    this.show = true
                })
            },
            onHandleUpdate(row) {
                this.type = 'edit'
                this.detailId = row.appMenuId
                this.title = '修改菜单'
                detailAppMenu({id: row.appMenuId}).then(res=> {
                    this.form = res.data
                    this.form.type = Number(res.data.type)
                    this.show = true
                })
            },
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delAppMenu(row.appMenuId).then(res => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList()
                    })
                   
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            
             /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.queryParams.firstType = ''
                this.queryParams.secondType = ''
                this.handleQuery();
            },
            getList() {
                this.loading = true;
                 
                listAppMenu(this.queryParams).then((response) => {
                    this.tableList = response.data.records;
                    this.total = response.data.total;
                    this.loading = false;
                });
            }
        }
    }
</script>